import time

from selenium.webdriver.common.by import By

from base.page_base import BasePage
from config import BASE_URL


class Register(BasePage):
    """注册页面封装"""

    # 类属性:元素定位
    __phone = (By.ID, "phone")
    __password = (By.ID, "password")
    __verifycode = (By.ID, "verifycode")
    __get_phone_code = (By.ID, "get_phone_code")
    __phone_code = (By.ID, "phone_code")
    __reg_btn = (By.CLASS_NAME, "lg-btn")

    # 注册成功文本元素
    __success_text = (By.XPATH, "//*[(text()='注册成功！')]")
    # 注册失败文本元素
    __fail_text = (By.CLASS_NAME, "reg-title")

    def open_url(self):
        self.driver.get(BASE_URL + "/common/member/reg")

    def input_phone(self, phone):
        """输入手机号"""
        self.base_input(self.__phone, phone)

    def input_password(self, password):
        """输入密码"""
        self.base_input(self.__password, password)

    def input_verifycode(self, verifycode="8888"):
        """输入图片验证码"""
        self.base_input(self.__verifycode, verifycode)

    def click_get_phone_code(self):
        """点击获取短信验证码"""
        self.base_click(self.__get_phone_code)

    def input_phone_code(self, phone_code="666666"):
        """输入短信验证码"""
        self.base_input(self.__phone_code, phone_code)

    def click_reg_btn(self):
        """点击注册按钮"""
        self.base_click(self.__reg_btn)

    def get_result_success_text(self):
        """获取注册成功文本"""
        return self.fd_element(self.__success_text).text

    def get_result_fail_text(self):
        """获取注册失败文本"""
        return self.fd_element(self.__fail_text).text

    def register(self, phone, password, verifycode="8888", phone_code="666666"):
        """注册流程方法"""
        self.input_phone(phone)
        self.input_password(password)
        self.input_verifycode(verifycode)
        self.click_get_phone_code()
        time.sleep(2)
        self.input_phone_code(phone_code)
        self.click_reg_btn()
